package com.cea.papyrus.core.ui.dialogs;

import java.util.Comparator;
import java.util.Iterator;

import org.eclipse.core.runtime.Assert;

/**
 * Creates a new ElementList with a special comparator. This comparator is used to modify the contains() method. 
 */
public class ElementListWithComparator extends ElementList {
	
	/** Comparator used by this list for the contains() method */
	protected Comparator comparator;
	
	/**
	 * Defaut constructor
	 * @param comparator the comparator used by this list
	 */
	public ElementListWithComparator(Comparator comparator) {
		Assert.isNotNull(comparator, "Comparator should not be null");
		this.comparator = comparator;
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.dialogs.ElementList#contains(java.lang.Object)
	 */
	@Override
	public boolean contains(Object element) {
		// for each element of the list, tries to find if the element already exists
		Iterator it = elements.iterator(); 
		while(it.hasNext()) {
			if(0 == comparator.compare(element, it.next())) {
				return true;
			}
		}
		return false;
	}
}
